package com.xuelang.pipeline.entry.resource;

import com.xuelang.pipeline.app.resource.ClusterService;
import com.xuelang.pipeline.app.resource.NamespaceService;
import com.xuelang.pipeline.app.resource.request.NamespaceListRequest;
import com.xuelang.pipeline.app.resource.request.NamespaceMainRequest;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.INamespaceService;
import com.xuelang.pipeline.client.resource.dto.ClusterDTO;
import com.xuelang.pipeline.client.resource.dto.NamespaceDTO;
import com.xuelang.pipeline.client.resource.dto.NamespaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 命名空间服务web控制类
 *
 * @author xiwen.zxg
 * @since 2024/5/28
 */
@Slf4j
@RestController
@RequestMapping("/namespace")
public class NamespaceController implements INamespaceService {

    @Autowired
    NamespaceService namespaceService;
    @Autowired
    ClusterService clusterService;

    @Override
    public ApiResult<List<NamespaceDTO>> conditionalNamespaceList(NamespaceVO namespaceVO) {
        ClusterDTO clusterDTO = clusterService.query(namespaceVO.getTargetClusterUuid());
        BizAssert.notNull(clusterDTO, "目标集群获取失败");
        BizAssert.notEmpty(clusterDTO.getKubeConfig(), "请配置集群凭证.");
        NamespaceListRequest namespaceListRequest = new NamespaceListRequest();
        namespaceListRequest.setKubeConfig(clusterDTO.getKubeConfig());
        return namespaceService.list(namespaceListRequest);
    }

    @Override
    public ApiResult<Boolean> addNamespace(NamespaceVO namespaceVO) {
        BizAssert.notNull(namespaceVO.getNamespaceName(), "命名空间名称不为空");
        ClusterDTO clusterDTO = clusterService.query(namespaceVO.getTargetClusterUuid());
        BizAssert.notNull(clusterDTO, "目标集群获取失败");
        BizAssert.notEmpty(clusterDTO.getKubeConfig(), "请配置集群凭证.");
        NamespaceMainRequest namespaceMainRequest = new NamespaceMainRequest();
        namespaceMainRequest.setKubeConfig(clusterDTO.getKubeConfig());
        namespaceMainRequest.setNamespaceName(namespaceVO.getNamespaceName());
        return namespaceService.create(namespaceMainRequest);
    }

    @Override
    public ApiResult<Boolean> deleteNamespace(NamespaceVO namespaceVO) {
        BizAssert.notNull(namespaceVO.getNamespaceName(), "命名空间名称不为空");
        ClusterDTO clusterDTO = clusterService.query(namespaceVO.getTargetClusterUuid());
        BizAssert.notNull(clusterDTO, "目标集群获取失败");
        BizAssert.notEmpty(clusterDTO.getKubeConfig(), "请配置集群凭证.");
        NamespaceMainRequest namespaceMainRequest = new NamespaceMainRequest();
        namespaceMainRequest.setKubeConfig(clusterDTO.getKubeConfig());
        namespaceMainRequest.setNamespaceName(namespaceVO.getNamespaceName());
        return namespaceService.delete(namespaceMainRequest);
    }
}
